﻿<%@ Page Title="Promotion Engine - Sales Person" Language="C#" MasterPageFile="~/Site.Master"
    AutoEventWireup="true" CodeBehind="HierarchyHeader.aspx.cs" Inherits="PromotionEngine.HierarchyHeader" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <script type="text/javascript">
        var oHierarchy = {};
        var isInsert = false;
        var gird;
        var girdDetail;
        var wnd, detailsTemplate;
        var jsonReference = {};
        var currentPage = "HierarchyHeader.aspx";
        function GetAllReference() {
            $.ajax({
                type: "GET",
                url: currentPage + "/GetAllReference",
                data: '',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    jsonReference = $.parseJSON(data.d)
                },
                failure: function (response) {
                    alert(response.d);
                },
                error: function (xhr, status, err) {
                    var err = eval("(" + xhr.responseText + ")");
                    alert(err.Message);
                }
            });
        }
        function showDetails(e) {
            e.preventDefault();
            var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
            var wnd = $("#details")
                        .kendoWindow({
                            title: "Hierarchy Detail",
                            modal: true,
                            visible: false,
                            resizable: false,
                            width: 700,
                            height: 550,
                            content: { type: "POST", url: "HierarchyDetailTree.aspx?HierarchyID=" + dataItem.HierarchyID + "&HierarchyName=" + dataItem.HierarchyName, data: { "property": kendo.stringify(dataItem)} },
                            iframe: true
                        }).data("kendoWindow");
            wnd.center().open();
            wnd.bind('close', function () {
                            $("#details").empty();
                        });
        }

        $(document).ready(function () {
            GetAllReference();
            //Load Grid
            grid = $("#grid").kendoGrid({
                dataSource: {
                    transport: {
                        read: {
                            type: "GET",
                            url: currentPage + "/GetHierarchyList",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json"
                        },
                        destroy: {
                            type: "GET",
                            url: currentPage + "/DeleteHierarchy",
                            contentType: "application/json; charset=utf-8",
                            dataType: "json"
                        },
                        parameterMap: function (data, operation) {
                            if (operation === "destroy") {
                                return { hierarchyID: data.HierarchyID };
                            }
                        },
                        serverPaging: true,
                        serverSorting: true,
                        serverFiltering: true
                    },
                    type: "odata", //Important!!!!
                    schema: {
                        data: function (a) {
                            if (a.d != null) {
                                var data = a.d.Data;
                                return data;
                            }
                        },
                        total: "d.Count",
                        model: {
                            id: "HierarchyID",
                            fields: {
                                HierarchyID: { type: "string", validation: { required: true} },
                                HierarchyCode: { type: "string", validation: { required: true} },
                                HierarchyName: { type: "string", validation: { required: true} },
                                CompanyCode: { type: "string" },
                                CompanyName: { type: "string" },
                                HierarchyType: { type: "string", validation: { required: true} },
                                ReferenceName: { type: "string" }
                            }
                        }
                    }
                },
                sortable: true,
                pageable: {
                    pageSize: 20,
                    refresh: true
                },
                filterable: true,
                reorderable: true,
                resizable: true,
                selectable: "row",
                height: 450,
                toolbar: [{ name: 'create', text: 'Add New Hiererchy' }, { name: 'excel'}],
                excel: {
                    fileName: "Sales Person List.xlsx",
                    filterable: true,
                    allPages: true
                },
                batch: true,
                columns: [{ field: "HierarchyID", title: "HierarchyID", width: 100, hidden: true },
                            { field: "HierarchyCode", title: "Code", width: 200 },
                            { field: "HierarchyName", title: "Name" },
                            { field: "HierarchyType", title: "Type", hidden: true },
                            { field: "ReferenceName", title: "Type ", width: 300 },
                            { field: "CompanyCode", title: "CompanyCode ", hidden: true },
                            { field: "CompanyName", title: "Company " },
                            { command: ["edit", "destroy",{ text: "Detail", click: showDetails}], title: "Action", width: "270px" }
                            ],
                editable: {
                    mode: "popup",
                    template: $("#template").html(),
                    confirmation: "Are you sure you want to remove this hierarchy?"
                },
                edit: function (e) {
                    $('#dllHierarchyType').kendoDropDownList({
                        autoBind: false,
                        filter: "contains",
                        optionLabel: "[Select Type]",
                        suggest: true,
                        dataTextField: "ReferenceName",
                        dataValueField: "ReferenceValue",
                        dataSource: {
                            data: jsonReference
                        }
                    });
                    var editWindow = e.container.data("kendoWindow");
                    if (e.model.isNew()) {
                        $('[name="HierarchyID"]').attr("readonly", true);
                        e.container.data("kendoWindow").title('Add New Hierarchy');
                        $("a.k-grid-update")[0].innerHTML = "<span class='k-icon k-update'></span>Save";
                        isInsert = true;
                    }
                    else {
                        e.container.data("kendoWindow").title('Edit Hierarchy');
                        isInsert = false;
                    }
                },
                remove: function (e) {
                },
                save: function (e) {
                    var listTextBox = $("input:text");
                    for (var i = 0; i < listTextBox.length; i++) {
                        var name = listTextBox[i].name;
                        if (name.length > 0) {
                            oHierarchy[name] = $("[name=" + name + "]").val();
                        }
                    }
                    oHierarchy.HierarchyType = $("#dllHierarchyType").data("kendoDropDownList").value();
                    //Update or Insert Company
                    $.ajax({
                        type: "POST",
                        url: currentPage + "/UpdateHierarchy",
                        data: JSON.stringify({
                            oHierarchy: JSON.stringify(oHierarchy),
                            isInsert: isInsert
                        }),
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            //Reload Data 
                            grid.dataSource.read();
                        },
                        error: function (xhr, status, err) {
                            var err = eval("(" + xhr.responseText + ")");
                            alert(err.Message);
                        }
                    });
                    this.refresh();
                }
            }).data("kendoGrid");
        });
    </script>
    <script type="text/x-kendo-template" id="template">  
       <div class="k-edit-label"  style="display: none;">
        <label for="HierarchyID">
            ID</label></div>
    <div class="k-edit-field" data-container-for="HierarchyID"  style="display: none;">
        <input name="HierarchyID" class="k-input k-textbox" required="required" type="text"
            data-bind="value:HierarchyID"></div>
    <div class="k-edit-label">
        <label for="HierarchyCode">
            Code</label></div>
    <div class="k-edit-field" data-container-for="HierarchyCode">
        <input name="HierarchyCode" class="k-input k-textbox" required="required" type="text" validationMessage = "Code is required"
            data-bind="value:HierarchyCode"></div>
    <div class="k-edit-label">
        <label for="HierarchyName">
            Name</label></div>
    <div class="k-edit-field" data-container-for="HierarchyName">
        <input name="HierarchyName" class="k-input k-textbox" required="required" type="text" validationMessage = "Name is required"
            data-bind="value:HierarchyName"></div>
    <div class="k-edit-label">
        <label for="HierarchyType">
            Type</label></div>
    <div data-container-for="HierarchyType" class="k-edit-field">
        <input id="dllHierarchyType" name="HierarchyType" data-bind="value:HierarchyType" required="required" validationMessage = "Type is required" />
    </div>
    <div class="k-edit-label" style="display: none;">
        <label for="CompanyCode">
            CompanyCode
        </label>
    </div>
    <div class="k-edit-field" data-container-for="CompanyCode" style="display: none;">
        <input name="CompanyCode" class="k-input k-textbox" type="text" data-bind="value:CompanyCode"></div>
    </script>
    <div id="details">
    </div>
    <div style="position: absolute; z-index: 0; margin-right: 8px;">
        <div id="grid">
        </div>
    </div>
</asp:Content>
